package org.jz.gae.redbook.spb.server;

import java.util.ArrayList;

import org.jz.gae.redbook.spb.client.RedBookService;
import org.jz.gae.redbook.spb.server.controller.DistrictController;
import org.jz.gae.redbook.spb.server.controller.DistrictControllerGoogle;
import org.jz.gae.redbook.spb.server.controller.FamilyController;
import org.jz.gae.redbook.spb.server.controller.FamilyControllerGoogle;
import org.jz.gae.redbook.spb.server.controller.LocationController;
import org.jz.gae.redbook.spb.server.controller.LocationControllerGoogle;
import org.jz.gae.redbook.spb.server.controller.SpeciesController;
import org.jz.gae.redbook.spb.server.controller.SpeciesControllerGoogle;
import org.jz.gae.redbook.spb.shared.DistrictEntityView;
import org.jz.gae.redbook.spb.shared.FamilyEntityView;
import org.jz.gae.redbook.spb.shared.LocationEntityView;
import org.jz.gae.redbook.spb.shared.SpeciesEntityView;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class RedBookServiceImpl extends RemoteServiceServlet implements RedBookService
{

	@Override
	public String greetServer(String name) throws IllegalArgumentException 
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FamilyEntityView addFamily(FamilyEntityView family) 
	{
	    FamilyController familyController = new FamilyControllerGoogle();
	    familyController.createFamily(family);
	    return family;
	}

	@Override
	public Boolean deleteFamily(String id) 
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<FamilyEntityView> getFamilyDetails() 
	{
		ArrayList<FamilyEntityView> result = new ArrayList<FamilyEntityView>();
		/**/
	    FamilyController familyController = new FamilyControllerGoogle();
	    familyController.loadFamilyList(result);
		/**/
	    return result; 
	}

	@Override
	public FamilyEntityView getFamily(String id) 
	{
		try
		{
		    FamilyController familyController = new FamilyControllerGoogle();
		    FamilyEntityView result = familyController.loadFamily(id);
		    return result;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public FamilyEntityView updateFamily(FamilyEntityView family) 
	{
		try
		{
	        FamilyController familyController = new FamilyControllerGoogle();
	        familyController.updateFamily(family);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	    return family;
	}

	@Override
	public LocationEntityView addLocation(LocationEntityView location) {
		LocationController locationController = new LocationControllerGoogle();
		locationController.createLocation(location);
		return location;
	}

	@Override
	public Boolean deleteLocation(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<LocationEntityView> getLocationDetails() 
	{
		ArrayList<LocationEntityView> result = new ArrayList<LocationEntityView>();
		/**/
	    LocationController locationController = new LocationControllerGoogle();
	    locationController.loadLocationList(result);
		/**/
	    return result; 
	}
	
	@Override
	public ArrayList<LocationEntityView> getLocationsForSpecies(
			String _SpeciesKey)
	{
		ArrayList<LocationEntityView> result = new ArrayList<LocationEntityView>();
		/**/
	    LocationController locationController = new LocationControllerGoogle();
	    locationController.loadLocationListForSpecies(
	    		_SpeciesKey,
	    		result);
		/**/
	    return result; 
	}

	@Override
	public LocationEntityView getLocation(String id) {
		try
		{
		    LocationController locationController = new LocationControllerGoogle();
		    LocationEntityView result = locationController.loadLocation(id);
		    return result;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public LocationEntityView updateLocation(LocationEntityView location) {
		try
		{
	        LocationController locationController = new LocationControllerGoogle();
	        locationController.updateLocation(location);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	    return location;
	}

	
	@Override
	public DistrictEntityView addDistrict(DistrictEntityView district) {
		DistrictController districtController = new DistrictControllerGoogle();
		districtController.createDistrict(district);
		return district;
	}

	@Override
	public Boolean deleteDistrict(String id) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ArrayList<DistrictEntityView> getDistrictDetails() {
		ArrayList<DistrictEntityView> result = new ArrayList<DistrictEntityView>();
		/**/
	    DistrictController districtController = new DistrictControllerGoogle();
	    districtController.loadDistrictList(result);
		/**/
	    return result; 
	}

	@Override
	public DistrictEntityView getDistrict(String id) {
		try
		{
		    DistrictController districtController = new DistrictControllerGoogle();
		    DistrictEntityView result = districtController.loadDistrict(id);
		    return result;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public DistrictEntityView updateDistrict(DistrictEntityView district) {
		try
		{
	        DistrictController districtController = new DistrictControllerGoogle();
	        districtController.updateDistrict(district);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	    return district;
	}

	
	@Override
	public SpeciesEntityView addSpecies(SpeciesEntityView species) 
	{
	    SpeciesController speciesController = new SpeciesControllerGoogle();
	    speciesController.createSpecies(species);
	    return species;
	}

	@Override
	public Boolean deleteSpecies(String id) 
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<SpeciesEntityView> getSpeciesDetails() 
	{
		ArrayList<SpeciesEntityView> result = new ArrayList<SpeciesEntityView>();
		/**/
	    SpeciesController speciesController = new SpeciesControllerGoogle();
	    speciesController.loadSpeciesList(result);
		/**/
	    return result; 
	}

	@Override
	public SpeciesEntityView getSpecies(String id) 
	{
		try
		{
		    SpeciesController speciesController = new SpeciesControllerGoogle();
		    SpeciesEntityView result = speciesController.loadSpecies(id);
		    return result;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			return null;
		}
	}

	@Override
	public SpeciesEntityView updateSpecies(SpeciesEntityView species) 
	{
		try
		{
	        SpeciesController speciesController = new SpeciesControllerGoogle();
	        speciesController.updateSpecies(species);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	    return species;
	}

}
